home *** CD-ROM | disk | FTP | other *** search
- /* HASAppleEvents.c from Hsoi's App Shell © 1995-1997 John C. Daub. All rights reserved.
-
- This file contains all the stuff to deal with AppleEvents: installing them,
- running them, etc.
-
- HAS supports the 4 required AppleEvents (tho it currently does nothing for
- print events) and also has some support for the GET and SET data events
- (see HASScripting.c and the 'aete' resource)
- */
-
- /* I need to give thanx to Reinder Verlinde (reinder@neuretp.biol.ruu.nl) for his
- help on getting AppleEvents to work. It was a simple (dumb) mistake (as a hint
- for better programming, don't do it late at night when you're tired and sleepy,
- cause you mess up like this).
-
- Simply enough, I was setting gQuitting = false; in HandleQUIT() (duh). Should
- be true. Thanx Reinder for pointing this out. Reinder also gave me a few good
- points for inserting DebugStr()'s to help check this out that I was even getting
- AppleEvents.
-
- John Daub March 24, 1995
- */
-
-
- #pragma mark ••• #includes •••
-
- #ifndef _WASTE_
- #include "WASTE.h"
- #endif
- #include "HASGlobals.h"
- #ifndef __HSOIS_APP_SHELL__
- #include "HASMain.h"
- #endif
- #include "HASAppleEvents.h"
- #include "HASScripting.h"
- #include "HASWindows.h"
- #include "HASUtilities.h"
-
- #pragma mark -
- #pragma mark ••• Globals •••
-
- // get us some UPP's for the apple event procs
-
- static AEEventHandlerUPP sODOCProc, sQUITProc, sPDOCProc, sOAPPProc;
-
-
- #pragma mark -
- #pragma mark ••• AE Installation •••
-
- /*
- * This installs the AppleEvent handles, and the TextServices Manager AppleEvents
- */
-
- void HsoiDoAEInstallation( void )
- {
- OSErr myErr = noErr;
-
- // allocate space for the mouse region
-
- // gCursorRgn = NewRgn();
-
- // get the handler procs
-
- if ( sODOCProc == nil )
- {
- sODOCProc = NewAEEventHandlerProc(HsoiHandleODOC);
- sQUITProc = NewAEEventHandlerProc(HsoiHandleQUIT);
- sPDOCProc = NewAEEventHandlerProc(HsoiHandlePDOC);
- sOAPPProc = NewAEEventHandlerProc(HsoiHandleOAPP);
- }
-
- // install the event handers
-
- myErr = AEInstallEventHandler( kCoreEventClass, kAEOpenDocuments, sODOCProc, 0, false );
- if ( myErr != noErr )
- HsoiDoError( rErrorStrings, errAEInstallFail, myErr, kErrDeath );
-
- myErr = AEInstallEventHandler( kCoreEventClass, kAEQuitApplication, sQUITProc, 0, false );
- if ( myErr != noErr )
- HsoiDoError( rErrorStrings, errAEInstallFail, myErr, kErrDeath );
-
- myErr = AEInstallEventHandler( kCoreEventClass, kAEPrintDocuments, sPDOCProc, 0, false );
- if ( myErr != noErr )
- HsoiDoError( rErrorStrings, errAEInstallFail, myErr, kErrDeath );
-
- myErr = AEInstallEventHandler( kCoreEventClass, kAEOpenApplication, sOAPPProc, 0, false );
- if ( myErr != noErr )
- HsoiDoError( rErrorStrings, errAEInstallFail, myErr, kErrDeath );
-
- // install Apple event handlers for a subset of the Core Suite
-
- myErr = HsoiInstallCoreHandlers();
- if ( myErr != noErr )
- HsoiDoError( rErrorStrings, errAEInstallFail, myErr, kErrDeath );
-
-
- // install AppleEvent Handlers for inline input
-
- if ( gHasTextServices )
- WEInstallTSMHandlers();
-
- return;
- }
-
- #pragma mark -
- #pragma mark ••• AE Handlers •••
-
- /*************************************************************/
- /* AppleEvent Handlers for the 4 required/core apple events */
- /*************************************************************/
-
- // Open a Document
-
- pascal OSErr HsoiHandleODOC( AppleEvent *theAppleEvent, AppleEvent *reply, long myRefCon )
- {
- #pragma unused ( reply, myRefCon )
-
- OSErr myErr = noErr;
- AEDescList docList;
- AEKeyword keyword;
- DescType returnedType;
- Size actualSize;
- long numberOfDocuments, i;
- FSSpec fileSpec;
- FInfo theFInfo;
- Boolean isStationery;
-
- // extract direct parameter from the Apple Event
-
- myErr = AEGetParamDesc( theAppleEvent, keyDirectObject, typeAEList, &docList );
- if ( myErr )
- {
- AEDisposeDesc( &docList );
- return( myErr );
- }
-
- // perform the recommended check for additional required parameters
-
- myErr = HsoiRequiredCheck( theAppleEvent );
- if (myErr )
- {
- AEDisposeDesc( &docList );
- return( myErr );
- }
-
- // count the items in the list of aliases
-
- myErr = AECountItems( &docList, &numberOfDocuments );
- if ( myErr )
- {
- AEDisposeDesc( &docList );
- return( myErr );
- }
-
- for ( i = 1; i <= numberOfDocuments; i++ )
- {
- // coerce the nth alias to a file system specification record
-
- myErr = AEGetNthPtr( &docList, i, typeFSS, &keyword, &returnedType, (Ptr)&fileSpec, sizeof( fileSpec ), &actualSize );
- if ( myErr )
- {
- AEDisposeDesc( &docList );
- return( myErr );
- }
-
- // open the specified file
-
- // i check for stationary....stationary isn't currently supported in the shell,
- // but will be in the future. so, this is just forethought.
-
- FSpGetFInfo( &fileSpec, &theFInfo);
- isStationery = ( (theFInfo.fdFlags & 0x0800 ) != 0 );
- myErr = HsoiCreateWindow( &fileSpec );
- if ( myErr )
- {
- AEDisposeDesc( &docList );
- return myErr;
- }
- } // end for loop
-
- // dispose of the alias list
-
- myErr = AEDisposeDesc( &docList );
- if ( myErr )
- {
- AEDisposeDesc( &docList ); // silly? but just to keep with Marco's code...
- return myErr;
- }
-
- return( noErr );
- }
-
-
-
- // Handle a quit
-
- pascal OSErr HsoiHandleQUIT( AppleEvent *theAppleEvent, AppleEvent *reply, long myRefCon )
- {
- #pragma unused ( reply, myRefCon )
-
- AEKeyword optKey;
- DescType actualType;
- Size actualSize;
- SavingOption saving = savingAsk; //default saving option is savingAsk
- OSErr myErr;
-
- // extract the optional save options
-
- myErr = AEGetKeyPtr( theAppleEvent, keyAESaveOptions, typeEnumerated, &actualType, &optKey, sizeof( optKey ), &actualSize );
-
- if ( myErr == noErr )
- {
- // determine if the option key was held down
-
- if ( optKey == kAEYes )
- saving = savingYes;
- else if ( optKey == kAENo )
- saving = savingNo;
- else if ( optKey == kAEAsk )
- {
- myErr = paramErr; // for want of a better code
- return myErr;
- }
- }
-
-
- // perform the recommended check for additional required parameters
-
- myErr = HsoiRequiredCheck( theAppleEvent );
- if ( myErr )
- return( myErr );
-
- // if clean up worked ok (e.g. all windows closed ok and saved etc...i.e. we do
- // want to quit and do all things to work towards that), then CleanUp should
- // return noErr, which == 0, therefore !0 should be 1, and gQuitting then
- // set to true, and we quit the program just peachy.
-
- gQuitting = !HsoiCleanUp( savingAsk );
-
- return( noErr );
- }
-
-
-
-
- // Launch the app
-
- pascal OSErr HsoiHandleOAPP( AppleEvent *theAppleEvent, AppleEvent *reply, long myRefCon )
- {
- #pragma unused ( reply, myRefCon )
-
- OSErr myErr;
-
- // perform the recommended check for additional required parameters
-
- myErr = HsoiRequiredCheck( theAppleEvent );
- if ( myErr )
- return ( myErr );
-
- // created a new window from scratch
-
- if ( gMyPrefs.createWindow )
- myErr = HsoiCreateWindow( nil );
-
- return( noErr );
- }
-
-
-
-
- // handle a print event, e.g. from the Finder
-
- // right now, I haven't written the print appleevent routines, as you can see. however,
- // it would be pretty easy to do. first, you'll have to put up the style and job dialogs
- // (Page Setup, and Print dialogs), get that info, and then open the document, get a
- // handle to the WASTE instance, and pass that to the normal printing routines.
- // i hope to have this implimented in a soon future release.
-
- pascal OSErr HsoiHandlePDOC( AppleEvent *theAppleEvent, AppleEvent *reply, long myRefCon )
- {
- #pragma unused ( theAppleEvent, reply, myRefCon )
-
- return( errAEEventNotHandled );
- }
-
-
- #pragma mark -
- #pragma mark ••• AE Utils •••
-
- // this performs the recommended check for additional required parameters
-
- OSErr HsoiRequiredCheck( AppleEvent *theAppleEvent )
- {
- OSErr myErr;
- DescType typeCode;
- Size actualSize;
-
- myErr = AEGetAttributePtr( theAppleEvent, keyMissedKeywordAttr, typeWildCard, &typeCode, nil, 0, &actualSize );
-
- if ( myErr == errAEDescNotFound)
- return( noErr );
-
- if ( myErr == noErr )
- return( errAEEventNotHandled );
-
- return( myErr );
- }
-